<?
class Person extends Persistable {
	public $mapping = array(
		"name" => array("type" => "VARCHAR", "size" => "255"),
		"username" => array("type" => "VARCHAR", "size" => "255", "unique" => "TRUE"),
		"email" => array("type" => "VARCHAR", "size" => "255"),
		"password" => array("type" => "VARCHAR", "size" => "255"),
	);

	protected $name;
	protected $username;
	protected $email;
	protected $password;

	public function Person($username = "", $pass = "", $name = null, $email = null){
		$this->username = strtolower($username);
		$this->password = $pass;
        $this->name = $name;
        $this->email = $email;
	}

	public function validateName() {
		if($this->name == "") return translate("name_cant_be_empty");
	}

	public function validateUsername() {
		if(isset($this->id)) return;

		$listUsers = db::get(User);
		for($i = 0; $i < count($listUsers); $i++){
			if($listUsers[$i]->getUsername() == $this->getUsername()){
				return translate("username_is_already_taken");
			}
		}
	}

	public function validateEmail() {
		if(!check_email_address($this->email)){
			return translate("email_is_not_valid");
		}
	}

	public function validatePassword(){
		if($this->password == "" || strlen($this->password) < 4){
			return translate("password_muss_have_more_than_4_characters");
		}
	}

	public function validateUserGroup(){
		if(!isset($this->userGroup_id) || $this->userGroup_id < 0){
			return translate("please_select_a_usergroup");
		}
	}
}
?>